﻿#include "Device.h"

Device::Device()
{
  connect(&timer_life,&QTimer::timeout,[this](){emit signal_lifeTimeOut(this);});
  connect(&timer_heartBeat,&QTimer::timeout,[this](){emit signal_heartBeatTimeout(this);});
  connect(&timer_currentState,&QTimer::timeout,[this](){emit signal_currentStateTimeout(this);});
  connect(&timer_runningState,&QTimer::timeout,[this](){emit signal_runningStateTimeout(this);});
}

Device::Device(const Device& device)
{
  *this=device;
}

Device::~Device()
{}

Device &Device::operator=(const Device &device)
{
  fromByteArray(this,(char*)&device.deviceId,byteArraySize);
  return *this;
}

bool Device::isValid()
{
  return deviceId;//设备id不为0则是合法的
}

QByteArray Device::toByteArray() const
{
  return QByteArray((char*)&deviceId,byteArraySize);
}

Device Device::fromByteArray(QByteArray data, bool *isOK)
{
  Device device;
  bool temp=false;
  if(Q_NULLPTR==isOK)
    {
      isOK=&temp;
    }
  *isOK = fromByteArray(&device,data.data(),data.size());
  return device;
}

QVector<Device> Device::fromByteArrays(QByteArray data , bool *isOK)
{
  int size=data.size();
  char* dataPtr=data.data();
  int offset=0;
  QVector<Device> devices;
  bool temp=false;
  if(Q_NULLPTR==isOK)
    {
      isOK=&temp;
    }
  while (size>offset)
    {
      Device device;
      if(fromByteArray(&device,dataPtr,size,&offset))
        {
          devices.append(device);
        }
      else
        {
          *isOK=false;
          return devices;
        }
    }
  *isOK=true;
  return devices;
}

bool Device::fromByteArray(Device* device, char* srcData, int srcSize, int* offset)
{
  int temp=0;
  if(Q_NULLPTR==offset)
    {
      offset=&temp;
    }
  if((srcSize-*offset)<byteArraySize)//如果数据长度不足则返回
    {
      return false;
    }
  else
    {
      char*src,*tar=(char*)&device->deviceId;
      src=srcData+*offset;
      *offset+=byteArraySize;
      for(uint i=0;i<byteArraySize;++i)
        {
          tar[i]=src[i];
        }
      return true;
    }
}

uint Device::getDeviceId()
{
  return deviceId;
}

Device* Device::setDeviceId(uint deviceId)
{
  this->deviceId=deviceId;
  return this;
}

uint Device::getHostAddress()
{
  return hostAddress;
}

Device* Device::setHostAddress(uint hostAddress)
{
  this->hostAddress=hostAddress;
  return this;
}

ushort Device::getPort()
{
  return port;
}

Device* Device::setPort(ushort port)
{
  this->port=port;
  return this;
}

int Device::getLifeTime_ms()
{
  int temp=timer_life.remainingTime();
  return temp<=0?0:temp;
}

Device* Device::setLifeTime_ms(int lifeTime)
{
  if(lifeTime<=0)
    {
      timer_life.stop();
      timer_life.setInterval(0);
    }
  else
    {
      timer_life.start(lifeTime);
    }
  return this;
}

int Device::getHeartBeatInterval_ms()
{
  return timer_heartBeat.interval();
}

Device* Device::setHeartBeatInterval_ms(int heartBeatInterval)
{
  if(heartBeatInterval<=0)
    {
      timer_heartBeat.stop();
      timer_heartBeat.setInterval(0);
    }
  else
    {
      timer_heartBeat.setInterval(heartBeatInterval);
      if(timer_heartBeat.remainingTime()>heartBeatInterval)
        {
          timer_heartBeat.start();
        }
      if(!timer_heartBeat.isActive())
        {
          timer_heartBeat.start();
        }
    }
  return this;
}

int Device::getCurrentStateInterval()
{
  return timer_currentState.interval();
}

Device* Device::setCurrentStateInterval(int currentStateInterval)
{
  if(currentStateInterval<=0)
    {
      timer_currentState.stop();
      timer_currentState.setInterval(0);
    }
  else
    {
      timer_currentState.start(currentStateInterval);
    }
  return this;
}

int Device::getRunningStateInterval()
{
  return timer_runningState.interval();
}

Device* Device::setRunningStateInterval(int runningStateInterval)
{
  if(runningStateInterval<=0)
    {
      timer_runningState.stop();
      timer_runningState.setInterval(0);
    }
  else
    {
      timer_runningState.start(runningStateInterval);
    }
}

float Device::getTurnTable_currentPosition()
{
  return turnTable_currentPosition;
}

Device* Device::setTurnTable_currentPosition(float turnTable_currentPosition)
{
  this->turnTable_currentPosition=turnTable_currentPosition;
  return this;
}

float Device::getTurnTable_currentRate()
{
  return turnTable_currentRate;
}

Device* Device::setTurnTable_currentRate(float turnTable_currentRate)
{
  this->turnTable_currentRate=turnTable_currentRate;
  return this;
}

float Device::getTurnTable_targetPosition()
{
  return turnTable_targetPosition;
}

Device* Device::setTurnTable_targetPosition(float turnTable_targetPosition)
{
  this->turnTable_targetPosition=turnTable_targetPosition;
  return this;
}

float Device::getTurnTable_targetRate()
{
  return turnTable_targetRate;
}

Device *Device::setTurnTable_targetRate(float turnTable_targetRate)
{
  this->turnTable_targetRate=turnTable_targetRate;
  return this;
}

float Device::getTurnTable_targetAcceleration()
{
  return turnTable_targetAcceleration;
}

Device* Device::setTurnTable_targetAcceleration(float turnTable_targetAcceleration)
{
  this->turnTable_targetAcceleration=turnTable_targetAcceleration;
  return this;
}

uchar Device::getTurnTable_runningState()
{
  return turnTable_runningState;
}

Device* Device::setTurnTable_runningState(uchar turnTable_runningState)
{
  this->turnTable_runningState=turnTable_runningState;
  return this;
}

bool Device::getTurnTable_runningState_targetPosition()
{
  return turnTable_runningState&mask_turnTable_runningState_targetPosition;
}

Device* Device::setTurnTable_runningState_targetPosition(bool turnTable_runningState_targetPosition)
{
  if(turnTable_runningState_targetPosition)
    {
      turnTable_runningState|=mask_turnTable_runningState_targetPosition;
    }
  else
    {
      turnTable_runningState&=~mask_turnTable_runningState_targetPosition;
    }
  return this;
}

bool Device::getTurnTable_runningState_targetRate()
{
  return turnTable_runningState&mask_turnTable_runningState_targetRate;
}

Device* Device::setTurnTable_runningState_targetRate(bool turnTable_runningState_targetRate)
{
  if(turnTable_runningState_targetRate)
    {
      turnTable_runningState|=mask_turnTable_runningState_targetRate;
    }
  else
    {
      turnTable_runningState&=~mask_turnTable_runningState_targetRate;
    }
  return this;
}

bool Device::getTurnTable_runningState_targetStop()
{
  return turnTable_runningState&mask_turnTable_runningState_targetStop;
}

Device* Device::setTurnTable_runningState_targetStop(bool turnTable_runningState_targetStop)
{
  if(turnTable_runningState_targetStop)
    {
      turnTable_runningState|=mask_turnTable_runningState_targetStop;
    }
  else
    {
      turnTable_runningState&=~mask_turnTable_runningState_targetStop;
    }
  return this;
}

bool Device::getTurnTable_runningState_power()
{
  return turnTable_runningState&mask_turnTable_runningState_power;
}

Device* Device::setTurnTable_runningState_power(bool turnTable_runningState_power)
{
  if(turnTable_runningState_power)
    {
      turnTable_runningState|=mask_turnTable_runningState_power;
    }
  else
    {
      turnTable_runningState&=~mask_turnTable_runningState_power;
    }
  return this;
}

bool Device::getTurnTable_runningState_circuit()
{
  return turnTable_runningState&mask_turnTable_runningState_circuit;
}

Device* Device::setTurnTable_runningState_circuit(bool turnTable_runningState_circuit)
{
  if(turnTable_runningState_circuit)
    {
      turnTable_runningState|=mask_turnTable_runningState_circuit;
    }
  else
    {
      turnTable_runningState&=~mask_turnTable_runningState_circuit;
    }
  return this;
}

bool Device::getTurnTable_runningState_amplifier()
{
  return turnTable_runningState&mask_turnTable_runningState_amplifier;
}

Device* Device::setTurnTable_runningState_amplifier(bool turnTable_runningState_amplifier)
{
  if(turnTable_runningState_amplifier)
    {
      turnTable_runningState|=mask_turnTable_runningState_amplifier;
    }
  else
    {
      turnTable_runningState&=~mask_turnTable_runningState_amplifier;
    }
  return this;
}

bool Device::getTurnTable_runningState_running()
{
  return turnTable_runningState&mask_turnTable_runningState_running;
}

Device* Device::setTurnTable_runningState_running(bool turnTable_runningState_running)
{
  if(turnTable_runningState_running)
    {
      turnTable_runningState|=mask_turnTable_runningState_running;
    }
  else
    {
      turnTable_runningState&=~mask_turnTable_runningState_running;
    }
  return this;
}

bool Device::getTurnTable_runningMode()
{
  return turnTable_runningState&mask_turnTable_runningMode;
}

Device* Device::setTurnTable_runningMode(bool turnTable_runningMod)
{
  if(turnTable_runningMod)//rate mode
    {
      turnTable_runningState|=mask_turnTable_runningMode;
    }
  else//position mode
    {
      turnTable_runningState&=~mask_turnTable_runningMode;
    }
  return this;
}

float Device::getTurnTable_thermostat_currentTemperature()
{
  return turnTable_thermostat_currentTemperature;
}

Device* Device::setTurnTable_thermostat_currentTemperature(float turnTable_thermostat_currentTemperature)
{
  this->turnTable_thermostat_currentTemperature=turnTable_thermostat_currentTemperature;
  return this;
}

float Device::getTurnTable_thermostat_targetTemperature()
{
  return turnTable_thermostat_targetTemperature;
}

Device* Device::setTurnTable_thermostat_targetTemperature(float turnTable_thermostat_targetTemperature)
{
  this->turnTable_thermostat_targetTemperature=turnTable_thermostat_targetTemperature;
  return this;
}

float Device::getTurnTable_thermostat_targetTemperatureRate()
{
  return turnTable_thermostat_targetTemperatureRate;
}

Device* Device::setTurnTable_thermostat_targetTemperatureRate(float turnTable_thermostat_targetTemperatureRate)
{
  this->turnTable_thermostat_targetTemperatureRate=turnTable_thermostat_targetTemperatureRate;
  return this;
}
